package com.phkj.qdh.ui.fragment.myfailover

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.phkj.qdh.R
import com.phkj.qdh.bean.MyFailoverListBean
import com.phkj.qdh.databinding.FragmentMyFailoverDetailBinding
import com.phkj.qdh.ui.adapter.FileTitleAdapter
import com.phkj.qdh.ui.adapter.ImagerViewAdapter
import com.phkj.qdh.ui.adapter.usercenter.MyFailoverThingsShowAdapter
import com.phkj.qdh.ui.fragment.user.UserVM
import com.phkj.qdh.ui.fragment.user.getUserVMProvider
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import kotlinx.coroutines.flow.collectLatest

/**
 *
 */
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class MyFailoverDetailFragment : Fragment() {
    private var param1: MyFailoverListBean? = null
    private var param2: String? = null
    private var _binding: FragmentMyFailoverDetailBinding? = null
    private val binding get() = _binding!!
    private lateinit var vm: UserVM
    private val iVAdapter by lazy { ImagerViewAdapter(true) }
    private val fileTitleAdapter by lazy { FileTitleAdapter() }
    private val failoverThingsShowAdapter by lazy { MyFailoverThingsShowAdapter() }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getParcelable(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentMyFailoverDetailBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getUserVMProvider())[UserVM::class.java]
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack()
            }
            tvType.text = param1?.faultTolerantTypeName
            rvShow.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = failoverThingsShowAdapter
                addItemDecoration(MyItemDecoration(width = 2))
            }
            rvPic.run {
                layoutManager =
                    LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
                adapter = iVAdapter
                addItemDecoration(
                    MyItemDecoration(
                        ContextCompat.getColor(
                            requireContext(), R.color.transparent
                        ), 28, RecyclerView.HORIZONTAL
                    )
                )
            }
            rvFile.run {
                layoutManager =
                    LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false)
                adapter = fileTitleAdapter
                addItemDecoration(
                    MyItemDecoration(
                        ContextCompat.getColor(
                            requireContext(), R.color.transparent
                        ), 28, RecyclerView.HORIZONTAL
                    )
                )
            }
        }

        launchAndRepeatWithViewLifecycle {
            vm.myFailoverDetail.collectLatest {
                binding.tvType1.text = it.eventTypeName
                binding.tvReson.text = it.reason
                binding.edtRemark.text = it.remark
                iVAdapter.submitList(it.pictureList)
                failoverThingsShowAdapter.submitList(it.eventList)
//                fileTitleAdapter.submitList(it.fileList.map {f-> UploadFileBean() })
            }
        }

        failoverThingsShowAdapter.addOnItemChildClickListener(R.id.tv_Detail) { adapter, _, pos ->
            val data = adapter.getItem(pos)
            if (data!!.inspectId==null){
                parentFragmentManager.commit {
                    addToBackStack("")
                    replace(id, MyFailoverBaseDetailFragment.newInstance(data!!.id, ""))
                    setReorderingAllowed(true)
                }
            }else{
                parentFragmentManager.commit {
                    addToBackStack("")
                    replace(id, MyFailoverTeamDetailFragment.newInstance(data!!.id, ""))
                    setReorderingAllowed(true)
                }
            }
        }
        vm.getMyFailoverDetail(param1!!.id)

    }

    companion object {
        @JvmStatic
        fun newInstance(param1: MyFailoverListBean, param2: String) =
            MyFailoverDetailFragment().apply {
                arguments = Bundle().apply {
                    putParcelable(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}